import scrapy

from caivideo.items import CaivideoItem


class CaiSpider(scrapy.Spider):
    name = 'cai'
    allowed_domains = ['www.meishij.net']
    start_urls = ['https://www.meishij.net/video/?page=1']

    def parse(self, response):
        base_url = 'https://www.meishij.net/video/?page={}'
        for i in range(1, 75):
            url = base_url.format(i)
            yield scrapy.Request(
                url=url,
                callback=self.parse2
            )

    def parse2(self, response):
        urls = response.xpath('//div[@class="v_list"]//a/@href').extract()  # 这个是每一页的url
        for url in urls:
            yield scrapy.Request(
                url=url,
                callback=self.parse3
            )

    def parse3(self, response):
        name = response.xpath('//div[@class="v_path"]/span/text()').extract()
        video = response.xpath('//div[@class="vw"]/video/@src').extract()
        yield CaivideoItem(
            name=name,
            video=video
        )
